package cn.zhentao.controller;

import cn.zhentao.book.dto.Booklist;
import cn.zhentao.book.pojo.TBook;
import cn.zhentao.common.Result;
import cn.zhentao.dtos.ResponseResult;
import cn.zhentao.service.FileStorageService;
import cn.zhentao.service.TBookService;
import cn.zhentao.user.pojo.TUser;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("tbook")
public class TBookControll {
    @Autowired
    private TBookService tBookService;
    @Autowired
    private FileStorageService fileStorageService;
    //查询图书以及所有条件
    @PostMapping("list")
    public Result list(@RequestBody Booklist booklist){
        Page<TBook> list = tBookService.listBook(booklist);
        return Result.OK(list);
    }

    //查询单个图书
    @GetMapping("findone")
    public Result findone(@RequestParam("id") Integer id){
        return tBookService.selctone(id);
    }
    private String bookurl=null;  //图书封面
    @PostMapping("avatar_submit")
    public Result avatar_submit(MultipartFile file) throws IOException {
        String path = fileStorageService.uploadImgFile("", file.getOriginalFilename(), file.getInputStream());
        System.out.println("图书封面======>"+path);
        bookurl=path;
        return Result.OK(path);
    }

    //图书添加
    @PostMapping("addbook")
    public Result addbook(@RequestBody TBook book){
      book.setUrl(bookurl);
      return tBookService.book_add(book);
    }

    //书上下架
    @PostMapping("book_status2")
    public ResponseResult book_status2(@RequestBody TBook book){
        return tBookService.book_upOrdown(book);
    }

    //租不租借图书
    @PostMapping("book_lease")
    public ResponseResult book_lease(@RequestBody TBook book){
        return tBookService.book_lease(book);
    }
}
